package com.lingyu.jsjx.service.impl;

import java.util.List;

import com.lingyu.jsjx.domain.TeachingReflection;
import com.lingyu.jsjx.mapper.TeachingReflectionMapper;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyu.jsjx.mapper.TeacherTenetMapper;
import com.lingyu.jsjx.domain.TeacherTenet;
import com.lingyu.jsjx.service.ITeacherTenetService;

import javax.validation.Validator;

/**
 * 教学心旨Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class TeacherTenetServiceImpl  extends ServiceImpl<TeacherTenetMapper, TeacherTenet> implements ITeacherTenetService
{
    @Autowired
    private TeacherTenetMapper teacherTenetMapper;
    private TeachingReflectionMapper teachingReflectionMapper;
    @Autowired
    private Validator validator;
    /**
     * 查询教学心旨
     * 
     * @param tenetId 教学心旨主键
     * @return 教学心旨
     */
    @Override
    public TeacherTenet selectTeacherTenetByTenetId(Long tenetId)
    {
        return teacherTenetMapper.selectTeacherTenetByTenetId(tenetId);
    }

    /**
     * 查询教学心旨列表
     * 
     * @param teacherTenet 教学心旨
     * @return 教学心旨
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "t",type = "jsjx")
    public List<TeacherTenet> selectTeacherTenetList(TeacherTenet teacherTenet)
    {
        return teacherTenetMapper.selectTeacherTenetList(teacherTenet);
    }

    /**
     * 新增教学心旨
     * 
     * @param teacherTenet 教学心旨
     * @return 结果
     */
    @Override
    public int insertTeacherTenet(TeacherTenet teacherTenet)
    {
        return baseMapper.insert(teacherTenet);
    }

    /**
     * 修改教学心旨
     * 
     * @param teacherTenet 教学心旨
     * @return 结果
     */
    @Override
    public int updateTeacherTenet(TeacherTenet teacherTenet)
    {

        return baseMapper.updateById(teacherTenet);
    }

    /**
     * 批量删除教学心旨
     * 
     * @param tenetIds 需要删除的教学心旨主键
     * @return 结果
     */
    @Override
    public int deleteTeacherTenetByTenetIds(Long[] tenetIds)
    {
        return teacherTenetMapper.deleteTeacherTenetByTenetIds(tenetIds);
    }

    /**
     * 删除教学心旨信息
     * 
     * @param tenetId 教学心旨主键
     * @return 结果
     */
    @Override
    public int deleteTeacherTenetByTenetId(Long tenetId)
    {
        return baseMapper.deleteById(tenetId);
    }

    @Override
    public String importTeacherTenet(List<TeacherTenet> teacherTenetList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(teacherTenetList) || teacherTenetList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (TeacherTenet teacherTenet : teacherTenetList) {
            try {

                teacherTenet.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                teacherTenet.setCreateByName(SecurityUtils.getUsername());
                teacherTenet.setCreateTime(DateUtils.getNowDate());

                // 验证数据合法性
                BeanValidators.validateWithException(validator, teacherTenet);
                teacherTenetMapper.insert(teacherTenet);
                successNum++;
                successMsg.append("<br/>" + successNum + "、教学心旨导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、教学心旨导导入失败";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }
}
